layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 实名认证表管理
     */
    var UserName = {
        tableId: "userNameTable"
    };

    /**
     * 初始化表格的列
     */
    UserName.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'userid', hide: true, title: '用户id'},
            {field: 'name', sort: true, title: '姓名'},
            {field: 'identity', sort: true, title: '身份证号码'},
            {field: 'justpic', sort: true, title: '身份证正面照',templet: function (d) {
                    var url =d.justpic;
                    //return '<img src="' +Feng.ctxPath+ url + '" class="tdImg" />';
                    return '<img src="' +Feng.ctxPath+ url + '" class="tdImg" onclick="showq(this)" />';
                }, title: '身份证正面照', unresize: true
            },

            {field: 'againstpic', sort: true, title: '身份证反面照',templet: function (d) {
                    var url =d.againstpic;
                    //return '<img src="' +Feng.ctxPath+ url + '" class="tdImg" />';
                    return '<img src="' +Feng.ctxPath+ url + '" class="tdImg" onclick="showq(this)" />';
                }, title: '身份证反面照', unresize: true
            },
            /*if (type == 0){
                        return "<span style='color: DarkTurquoise   '><b>否</b></span>";
                    }else if (type == 1){
                        return "<span style='color: DarkViolet '><b> 是</b></span>";
                    }else {
                        return "<span style='color: red '><b> 参数出现错误请删除重新定义</b></span>";
                    }*/
            {field: 'status', sort: true, title: '审核状态',templet: function (d) {
                    var url =d.status;
                    return url == 0 ? "<span style='color: yellow   '><b>审核中</b></span>" : url == 1 ? "<span style='color: red '><b>审核通过</b></span>" : "<span style='color: green '><b>审核不通过</b></span>";
                },
            },
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    UserName.search = function () {
        var queryData = {};
        queryData['id'] = $("#condition").val();
        table.reload(UserName.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    UserName.openAddDlg = function () {
        func.open({
            title: '添加实名认证表',
            content: Feng.ctxPath + '/userName/add',
            tableId: UserName.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    UserName.openEditDlg = function (data) {
        func.open({
            title: '修改实名认证表',
            content: Feng.ctxPath + '/userName/edit?id=' + data.id,
            tableId: UserName.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    UserName.exportExcel = function () {
        var checkRows = table.checkStatus(UserName.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    UserName.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/userName/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(UserName.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + UserName.tableId,
        url: Feng.ctxPath + '/userName/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: UserName.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        UserName.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        UserName.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        UserName.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + UserName.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            $.ajax({
                url: Feng.ctxPath + "/userName/upStatus",
                type: 'post',
                data:{
                    "id": data.id,
                    "status": "1"
                },
                dataType: "json",
                success: function(data) {
                    //eduData = data.data;
                    console.log(data);
                    alert(data.message);
                    window.location.reload();
                },
                error: function(xhr, textStatus) {

                }
            })
        } else if (layEvent === 'delete') {
            $.ajax({
                url: Feng.ctxPath + "/userName/upStatus",
                type: 'post',
                data:{
                    "id": data.id,
                    "status": "2"
                },
                dataType: "json",
                success: function(data) {
                    //eduData = data.data;
                    console.log(data);
                    alert(data.message);
                    window.location.reload();
                },
                error: function(xhr, textStatus) {

                }
            })
        }
    });
});
